package com.foruo.sc.upload.file.server.config;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * 文件读取配置
 * @author GaoYuan
 * @date 2018/4/10 上午11:01
 */
@Configuration
public class ReadConfig extends WebMvcConfigurerAdapter {

    //获取配置文件中图片的路径
    @Value("${raising.upload.imagesPath}")
    private String mImagesPath;
    //访问图片方法
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        if("".equals(mImagesPath) || "${raising.imagesPath}".equals(mImagesPath)){
            String imagesPath = ReadConfig.class.getClassLoader().getResource("").getPath();
            if(imagesPath.indexOf(".jar")>0){
                imagesPath = imagesPath.substring(0, imagesPath.indexOf(".jar"));
            }else if(imagesPath.indexOf("classes")>0){
                imagesPath = "file:"+imagesPath.substring(0, imagesPath.indexOf("classes"));
            }
//            imagesPath = imagesPath.substring(0, imagesPath.lastIndexOf("/"))+"/file/";
            imagesPath = imagesPath.substring(0, imagesPath.lastIndexOf("/"))+"/";
            mImagesPath = imagesPath;
        }else if(!mImagesPath.startsWith("file:")){
            mImagesPath = "file:" + mImagesPath;
        }
        LoggerFactory.getLogger(ReadConfig.class).info("imagesPath="+mImagesPath);
//        registry.addResourceHandler("/file/**").addResourceLocations(mImagesPath);
        registry.addResourceHandler("/**").addResourceLocations(mImagesPath);
        super.addResourceHandlers(registry);
    }

}
